package com.ihr360.job.core.repository.dao;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.sql.Types;

/**
 * Encapsulates common functionality needed by JDBC batch metadata DAOs -
 * provides jdbcTemplate for subclasses and handles table prefixes.
 *
 * @author Robert Kasanicky
 */
public abstract class AbstractJdbcBatchMetadataDao implements InitializingBean {

    /**
     * Default value for the table prefix property.
     */
    public static final String DEFAULT_TABLE_PREFIX = "BATCH_";

    public static final int DEFAULT_EXIT_MESSAGE_LENGTH = 2500;

    private String tablePrefix = DEFAULT_TABLE_PREFIX;

    private int clobTypeToUse = Types.CLOB;

    private JdbcOperations jdbcTemplate;

    protected String getQuery(String base, String in) {
        String replacePrefix = StringUtils.replace(base, "%PREFIX%", tablePrefix);
        return StringUtils.replace(replacePrefix, "%IN%", in);
    }

    protected String getQuery(String base, String in, String pageSize) {
        String replacePrefix = StringUtils.replace(base, "%PREFIX%", tablePrefix);
        String replaceIn = StringUtils.replace(replacePrefix, "%IN%", in);
        return StringUtils.replace(replaceIn, "%PAGESIZE%", pageSize);
    }

    protected String getQuery(String base) {
        return StringUtils.replace(base, "%PREFIX%", tablePrefix);
    }

    protected String getQuery(String base, int page, int size) {
        String replacePrefix = StringUtils.replace(base, "%PREFIX%", tablePrefix);
        String replaceOffset = StringUtils.replace(replacePrefix, "%OFFSET%", String.valueOf((page - 1) * size));
        return StringUtils.replace(replaceOffset, "%PAGESIZE%", String.valueOf(size));
    }

    protected String getQuery(String base, int page, int size, int interval) {
        String replacePrefix = StringUtils.replace(base, "%PREFIX%", tablePrefix);
        String replaceOffset = StringUtils.replace(replacePrefix, "%OFFSET%", String.valueOf((page - 1) * size));
        replaceOffset = StringUtils.replace(replaceOffset, "%INTERVAL%", String.valueOf(interval));
        return StringUtils.replace(replaceOffset, "%PAGESIZE%", String.valueOf(size));
    }

    protected String getQuery(String base, int interval) {
        String replacePrefix = StringUtils.replace(base, "%PREFIX%", tablePrefix);
        return StringUtils.replace(replacePrefix, "%INTERVAL%", String.valueOf(interval));
    }

    protected String getTablePrefix() {
        return tablePrefix;
    }

    /**
     * Public setter for the table prefix property. This will be prefixed to all
     * the table names before queries are executed. Defaults to
     * {@link #DEFAULT_TABLE_PREFIX}.
     *
     * @param tablePrefix the tablePrefix to set
     */
    public void setTablePrefix(String tablePrefix) {
        this.tablePrefix = tablePrefix;
    }

    public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    protected JdbcOperations getJdbcTemplate() {
        return jdbcTemplate;
    }

    public int getClobTypeToUse() {
        return clobTypeToUse;
    }

    public void setClobTypeToUse(int clobTypeToUse) {
        this.clobTypeToUse = clobTypeToUse;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(jdbcTemplate);
    }

}
